package com.eqdd.yiqidian.ui.mine.pizhun.Fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseShowFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CarPaperListBean;
import com.eqdd.yiqidian.bean.rebuild.NoticeListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.mine.pizhun.CarPaperInfoActivity;
import com.eqdd.yiqidian.utils.SlimUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by lvzhihao on 17-7-19.
 */

public class CarCheckFragment extends BaseShowFragment {
    private ModelRx2DataSource<CarPaperListBean> dataSource;
    private MVCCoolHelper<List<CarPaperListBean>> mvcHelper;
    private int pageNum = 0;

    @Override
    protected void setView() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().attachTo(dataBinding.recyclerView).updateData(new ArrayList());
            SlimUtil.layout(slimAdapterEx, R.layout.list_item_12, CarPaperListBean.class);
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        Bundle bundle = new Bundle();
                        bundle.putInt(Config.ID, ((CarPaperListBean) slimAdapterEx.getData().get(position)).getId());
                        bundle.putInt(Config.STATUS, status);
                        bundle.putInt(Config.WHO, who);
                        ((BaseActivity) getActivity()).startActivityForResult(CarPaperInfoActivity.class, bundle, position);

                    });
            dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    String url = "";
                    if (who == 1) {
                        url = HttpConfig.CAR_CHECK_LIST;
                    } else if (who == 2) {
                        url = HttpConfig.CAR_HR_CHECK_LIST;
                    }
                    return OkGo.<ZhangPageResultBean<List<CarPaperListBean>>>post(HttpConfig.BASE_URL + url)
                            .params("page", pageNum)
                            .params("comid", AppConstent.user.getCompanyId())
                            .params("userGuid", AppConstent.user.getGuid())
                            .params("type", status)
                            .converter(new JsonConverter<ZhangPageResultBean<List<CarPaperListBean>>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<>())
                            .flatMap(noticeWraperResultBeanHttpResult -> {
                                pageNum = page == 1 ? 0 : noticeWraperResultBeanHttpResult.getNextpage();
                                return Flowable.just(noticeWraperResultBeanHttpResult.getItems() == null ? new ArrayList<NoticeListResultBean>() : noticeWraperResultBeanHttpResult.getItems());
                            });

                }
            }, 10);
            mvcHelper = new MVCCoolHelper<List<CarPaperListBean>>(dataBinding.coolRefreshView);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }

    @Override
    protected void refresh() {
        mvcHelper.refresh();
    }
}
